package com.qdy.api;

import com.jiai.core.tools.RestResultGenerator;
import com.jiai.core.util.ErrorCode;
import com.jiai.exception.ServiceException;
import com.qdy.entity.team.Team;
import com.qdy.entity.team.TeamMember;
import com.qdy.entity.user.User;
import com.qdy.repository.TeamMemberRepository;
import com.qdy.repository.TeamRepository;

import com.qdy.service.TeamService;
import com.qdy.util.HttpUtil;
import com.qdy.util.UploadUtil;
import com.qdy.util.geo.GeoHash;
import com.sun.xml.internal.ws.developer.ServerSideException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 团队API接口
 * @version v1
 * Created by 方礼爱 on 2017/3/26.
 */
@RequestMapping(value = "/api/v1/team")
@RestController
public class TeamAPIController {
     @Autowired
     private TeamRepository teamRepository;

    /**
     * 团队成员接口
     */
    @Autowired
     private TeamMemberRepository teamMemberRepository;
    @Autowired
    private TeamService teamService;

    /**
     * 创建团队
     * @param team
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object createTeam(Team team, @RequestParam("file")MultipartFile file)throws ServiceException {
        try{
            User user=new User();
            user.setId(HttpUtil.getUserId());
            team.setTeamIcon(UploadUtil.copyFile(file));
            team.setUser(user);
            team.setCreateTime(new Date());

            team.setGeohash(new GeoHash(Double.valueOf(team.getLatitude()),Double.valueOf(team.getLongitude())).getGeoHashBase32());
            teamRepository.save(team);
            return RestResultGenerator.genSuccessResult("创建成功");
        }catch (Exception e){
            throw new ServiceException("创建失败",e.getMessage());
        }

    }
    @RequestMapping(value = "/getTeamInfo/{teamId}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object getTeamInfo(@PathVariable("teamId") Long teamId)throws ServiceException {
            try {
                 return RestResultGenerator.genSuccessResult(teamService.findByTeamId(teamId,HttpUtil.getUserId()));
            }catch (Exception e){
                throw new ServiceException("加载团队信息失败",e.getMessage());
            }
    }

    /**
     * 加载团队信息
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/load",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object loadAll()throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(teamService.findList(HttpUtil.getUserId()));
        }catch (Exception e){
            throw new ServiceException("加载团队信息失败",e.getMessage());
        }
    }


    /**
     * 审核成员
     * @return
     */

    @RequestMapping(value = "/changeStatus/{menberId}/{status}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object changeStatus(@PathVariable("menberId") Long menberId,@PathVariable("status") Integer status)throws ServiceException {
     try{
            teamMemberRepository.changeStatus(menberId,status);
            return RestResultGenerator.genSuccessResult();
         }catch (Exception e){
            throw new ServiceException("审核失败",e.getMessage());
        }



    }
    @RequestMapping(value="/exitTeam/{teamId}",method = RequestMethod.GET,produces={"application/json;charset=UTF-8"})
    public Object cancelApply(@PathVariable("teamId") Long teamId)throws ServiceException{
        try {
            teamMemberRepository.caenelApply(teamId,HttpUtil.getUserId());
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
                throw new ServiceException(e.getMessage());
        }
    }


    /**
     * 申请加入团队
     * @param teamId
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value="/apply/{teamId}",method = RequestMethod.GET,produces={"application/json;charset=UTF-8"})
    public Object apply(@PathVariable("teamId") Long teamId)throws ServiceException {
        try{
             TeamMember member=new TeamMember();
             member.setStatus(0);
             User user=new User();
             user.setId(HttpUtil.getUserId());
             member.setUser(user);
             member.setTeamId(teamId);
            teamMemberRepository.save(member);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
           throw new ServiceException(e.getMessage());
        }

    }

}
